import Request from '@/libs/request'
import qs from 'qs'
import { selectItemByTypeSign } from './information'

/**查询所有参数类型 */
export const selectAllDataType = () => Request.get<SysDict.Item[]>('/selectAllDataType')

export const addDevType = (data: AddDevType.Form) => Request.post<null>('/mg/addDevType', data)

/**分页查询设备列表 */
export const mSelectDevListByPage = (
  data: {
    userPhone?: UserPhone
    userCompany?: UserCompany
    devGId?: DevGId
    devTypeId?: DevTypeId | string
    devName?: DevName
    devStatusCode?: DevStatus
  } & Paging
) => Request.get<PagingResponse<Device.DataSource>>('/mg/select/devList', data)

/**查询设备所有数据 */
export const selectDetailData = (data: { devId: DevId }) =>
  Request.get<Device.DevBaseInfo>('/mg/select/devDetailData', data)

/**查询设备的通讯协议 */
export const selectDevConConfig = (data: { devId: DevId }) =>
  Request.get<Device.ConConfig>('/mg/select/devConConfig', data)

/**管理员解绑设备 */
export const mDisBindDev = (data: { platFormUserId: string | number; devId: DevId }) =>
  Request.get<null>('/mg/edit/mDisBindDev', data)

/**判断设备检测类型,是固定参数，还是非固定参数 */
export const mSelectDevIsFixedParam = (data: { devId: DevId }) =>
  Request.get<DevId>('/mg/mSelectDevIsFixedParam', data)

/**分页查询设备历史检测数据 固定参数和非固定参数都可用*/
export const mGetDevCheckHistoryData = (
  data: {
    devId: DevId
    startDate: string
    endDate: string
  } & Paging
) => Request.get<PagingResponse<Device.HistoryCheckData>>('/mg/mGetDevCheckHistoryData', data)

/**分页查询设备历史检测数据 固定参数*/
export const mGetDevCheckHistoryDataByParam = (
  data: Paging & {
    devId: DevId
    startDate: string
    endDate: string
    paramName: ParamName
  }
) => Request.get<PagingResponse<Device.CheckData>>('/mg/mGetDevCheckHistoryDataByParam', data)

/**查询设备某个参数所有历史检测数据 固定参数*/
export const mGetDevCheckDataNotPage = (data: {
  devId: DevId
  startDate: string
  endDate: string
  paramName: ParamName
}) => Request.get<Device.CheckData[]>('/mg/mGetDevCheckDataNotPage', data)

/**查询设备的检测参数 必须是固定参数*/
export const mSelectDevParam = (data: { devId: DevId }) =>
  Request.get<Device.CheckParam[]>('/mg/mSelectDevParam', data)

/**查询生产列表 */
export const mSelectProdDeviceByPage = (
  data: Paging & {
    startDate: string
    endDate: string
  }
) => Request.get<PagingResponse<Device.ProductDev>>('/mg/mSelectProdDeviceByPage', data)

/**添加生产设备 */
export const mAddProDevice = (data: Device.AddProductDevDataSource) =>
  Request.post<null>('/mg/mAddProDevice', qs.stringify(data))

/**通过devGId获取单个设备信息 */
export const eSelectDevByDevGId = (data: { devGId: DevGId }) =>
  Request.get<Device.DataSource>('/exc/eSelectDevByDevGId', data)

/**通过devGId获取websocket连接地址 */
export const eSelectWebServerAddress = (data: { devGId: DevGId }) =>
  Request.get<WebscoketAddress>('/exc/eSelectWebServerAddress', data)

/**通过userId查询设备列表 */
export const mSelectMyDevListByUserId = (data: { userId: UserId }) =>
  Request.get<Device.DevMsg>('/mg/mSelectMyDevListByUserId', data)

/**导出历史检测记录 */
export const exportCheckData = (data: { devId: DevId; startTime?: string; endTime?: string }) =>
  Request.filePost('/exportCheckData', qs.stringify(data), {
    responseType: 'blob'
  })

/**修改 基本信息 */
export const mEditDevData = (data: Device.EditDevMsgDataSource) =>
  Request.get<null>('/mg/mEditDevData', data)

/**删除 基本信息 */
export const mDelDevData = (data: { devDataId: number }) =>
  Request.get<null>('/mg/mDelDevData', data)

/**查询设备类型 */
export const mSelectDevTypeByPage = (data: { devTypeName?: string } & Paging) =>
  Request.get<PagingResponse<DeviceType.SimpleType>>('/mg/mSelectDevTypeByPage', data)

export type EditDevConConfigOptionParamConfDataJSON = Device.ParamConfDataJSON & {
  confId: number | undefined
  confDataId: number | undefined
  makeProperty: MakeProperty
}
export type EditDevConConfigOptionParamConfDataMODBUS = Device.ParamConfDataMODBUS & {
  confId: number | undefined
  confDataId: number | undefined
  makeProperty: MakeProperty
}
export type EditDevConConfigOptionParamConfJSON =
  Device.ParamConfJSON<EditDevConConfigOptionParamConfDataJSON> & {
    makeProperty: MakeProperty
  }

export type EditDevConConfigOptionParamConfMODBUS =
  Device.ParamConfMODBUS<EditDevConConfigOptionParamConfDataMODBUS> & {
    makeProperty: MakeProperty
  }

export type EditDevConConfigOptionParamConf =
  | EditDevConConfigOptionParamConfJSON
  | EditDevConConfigOptionParamConfMODBUS

export type EditDevConConfigOption<T = EditDevConConfigOptionParamConf> = Device.ConConfig<T>

/**管理员修改设备传输配置 */
export const editDevConConfig = (data: EditDevConConfigOption) =>
  Request.get<null>('/mg/edit/devConConfig', data)

/**管理员查询设备类型传输协议 */
export const selectDevTypeConConfig = (data: { devTypeId: number }) =>
  Request.get<DeviceType.ConConfig>('/mg/select/devTypeConConfig', data, {
    errorMessage: false
  })

/**管理员复制设备类型 */
export const addCopyDevType = (data: { devTypeId: number; newDevTypeName: string }) =>
  Request.get<null>('/mg/add/copyDevType', data, { successMessage: false })

/**管理员查询设备类型全部数据 */
export const selectDevTypeData = (data: { devTypeId: number }) =>
  Request.get<DeviceType.DevTypeConfig>('/mg/select/devTypeData', data)

/**查询设备通信类型列表 */
export const selectDevTxTypeList = () => selectItemByTypeSign({ dataType: 'devTypeTxType' })

/**查询设备通信协议类型列表 */
export const selectDevDataTypeList = () => selectItemByTypeSign({ dataType: 'devTypeDataType' })

/**查询设备基本信息列表 */
export const selectDevBaseDataList = () => selectItemByTypeSign({ dataType: 'devTypeMsgs' })

export type EditDevTypeConConfigOptionParamConfDataJSON = DeviceType.ParamConfDataJSON & {
  makeProperty: MakeProperty
}
export type EditDevTypeConConfigOptionParamConfDataMODBUS = DeviceType.ParamConfDataMODBUS & {
  makeProperty: MakeProperty
}
export type EditDevTypeConConfigOptionParamConfJSON =
  DeviceType.ParamConfJSON<EditDevTypeConConfigOptionParamConfDataJSON> & {
    makeProperty: MakeProperty
  }

export type EditDevTypeConConfigOptionParamConfMODBUS =
  DeviceType.ParamConfMODBUS<EditDevTypeConConfigOptionParamConfDataMODBUS> & {
    makeProperty: MakeProperty
  }

export type EditDevTypeConConfigOptionParamConf =
  | EditDevTypeConConfigOptionParamConfJSON
  | EditDevTypeConConfigOptionParamConfMODBUS

export type EditDevTypeConConfigOption = DeviceType.ConConfig<EditDevTypeConConfigOptionParamConf>
/**管理员修改设备类型传输协议 */
export const editDevTypeConConfig = (data: { devTypeConConfigData: EditDevTypeConConfigOption }) =>
  Request.post<null>('/mg/edit/devTypeConConfig', data)
